package com.metagurukul.metaspace.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.metagurukul.metaspace.models.beans.UserLoginDetail;
import com.metagurukul.metaspace.util.Constants;

/**
 * @author rahul gupta
 * to manage login details of a user
 */
public class UserLoginDetailDao extends DatabaseDAO {
	
	/**to get all list of users 
	 * @return list of user 
	 * @throws SQLException
	 */
	public List<UserLoginDetail> getRecordList ()
	throws SQLException {
		ArrayList<UserLoginDetail> userLoginDetailList = null;
		String query = "Select * from user_login_detail";
		try {
			getConnection();
			ps = con.prepareStatement(query);
			/**
			 * set variables in prepared statement
			 * 
			 */
			rs = ps.executeQuery();
			userLoginDetailList = new ArrayList<UserLoginDetail>();
			while (rs.next()) {
				UserLoginDetail userLoginDetail = fillBean();
				userLoginDetailList.add(userLoginDetail);
			}
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		return userLoginDetailList;
		
	}
	
	/**to fill bean of userlogindetail
	 * @return object of userlogindetail
	 * @throws SQLException
	 */
	private UserLoginDetail fillBean() throws SQLException{
		UserLoginDetail userLoginDetail = new UserLoginDetail();
		userLoginDetail.setUserId(rs.getInt("user_id"));
		userLoginDetail.setEmailId(rs.getString("email_id"));
		userLoginDetail.setPassword(rs.getString("password"));
		userLoginDetail.setisBlocked(rs.getInt("is_blocked"));
		return userLoginDetail;
	}
	
	/**to retrieve login detail of a particular user
	 * @param userId id of user
	 * @return  userlogin detail
	 * @throws SQLException
	 */
	public UserLoginDetail getRecord(int userId)
	throws SQLException {
		String query = "Select * from user_login_detail where user_id = ? ";
		try {
			getConnection();
			ps = con.prepareStatement(query);
			/**
			 * set variables in prepared statement
			 * 
			 */
			ps.setInt(1, userId);
			rs = ps.executeQuery();
			if (rs.next()) {
				
				return fillBean();
			}
			return null;
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		
		
	}
	
	/** to update user login detail
	 * @param userLoginDetail 
	 * @param oldPassword
	 * @return
	 * @throws SQLException
	 */
	public int updateRecord(UserLoginDetail userLoginDetail, String oldPassword) throws SQLException {
		// TODO Auto-generated method stub
		getConnection();
		String query = "update user_login_detail set password=? where user_id =? and password = ?";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setString(1, userLoginDetail.getPassword());
			ps.setInt(2, userLoginDetail.getUserId());
			ps.setString(3, oldPassword);
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}
	
	/**
	 * @param userLoginDetail
	 * @return
	 * @throws SQLException
	 */
	public int updateRecord(UserLoginDetail userLoginDetail) throws SQLException{
		if(userLoginDetail.getUserId()==-1){
			return updateRecordByEmailId(userLoginDetail);
		}
		else{
			return updateRecordByUserId(userLoginDetail);
		}
	}
	
	/**
	 * forgot Password
	 * @param userLoginDetail
	 * @return
	 * @throws SQLException
	 */
	public int updateRecordByEmailId(UserLoginDetail userLoginDetail) throws SQLException {
		// TODO Auto-generated method stub
		getConnection();
		String query = "update user_login_detail set password=? where email_id =? ";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setString(1, userLoginDetail.getPassword());
			ps.setString(2, userLoginDetail.getEmailId());
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}

	/**
	 * Resend Password
	 * @param userLoginDetail
	 * @return
	 * @throws SQLException
	 */
	public int updateRecordByUserId(UserLoginDetail userLoginDetail) throws SQLException {
		// TODO Auto-generated method stub
		getConnection();
		String query = "update user_login_detail set password=? where user_id =? ";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setString(1, userLoginDetail.getPassword());
			ps.setString(2, userLoginDetail.getEmailId());
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}
	
	/**
	 * @param userLoginDetail
	 * @return
	 * @throws SQLException
	 */
	public int insertRecord(UserLoginDetail userLoginDetail) throws SQLException {
		/* get the connection */
		getConnection();
		String query = "insert into user_login_detail (email_id, password) values( ? , ?)";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setString(1, userLoginDetail.getEmailId());
			ps.setString(2, userLoginDetail.getPassword());
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		
	}

	/**
	 * @param userId
	 * @return
	 * @throws SQLException
	 */
	public boolean checkIsPresent(int userId) throws SQLException {
		getConnection();
		String query = "select 1 from user_login_detail where user_id=?";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setInt(1, userId);
			rs = ps.executeQuery();
			/**
			 * if there is record in resultset then return true else return
			 * false
			 */
			 return rs.next();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}
	
	/**
	 * @param emailId
	 * @return
	 * @throws SQLException
	 */
	public boolean checkIsPresent(String emailId) throws SQLException {
		getConnection();
		String query = "select 1 from user_login_detail where email_id=?";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setString(1, emailId);
			rs = ps.executeQuery();
			/**
			 * if there is record in resultset then return true else return
			 * false
			 */
			return rs.next();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR  + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}
	
	/**
	 * @param emailId
	 * @return
	 * @throws SQLException
	 */
	public int getUserId(String emailId) throws SQLException {
		getConnection();
		String query = "select user_id from user_login_detail where email_id=?";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setString(1, emailId);
			rs = ps.executeQuery();
			/**
			 * if there is record in resultset then return true else return
			 * false
			 */
			if (rs.next()) {
				return rs.getInt(1);
			}
			return -1;
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR  + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}
	
	
	/**
	 * @param emailId
	 * @return
	 * @throws SQLException
	 */
	public int deleteRecord(String emailId) throws SQLException {
		/* get the connection */
		getConnection();
		String query = "delete from user_login_detail where email_id =?";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setString(1, emailId);
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR  + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		
	}
	/**
	 * @param userLoginDetail
	 * @return
	 * @throws SQLException
	 */
	public boolean loginValidate(UserLoginDetail userLoginDetail)
	throws SQLException {

		String query = "Select 1 from User_Login_Detail where email_id=? and password=?"; // query
		
		try {
			con = ConnectionDAO.getConnection();
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setString(1, userLoginDetail.getEmailId());
			ps.setString(2, userLoginDetail.getPassword());
			rs = ps.executeQuery();
			/**
			 * if there is record in resultset then return true else return
			 * false
			 */
			return rs.next();
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}
}
